home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dev / insight_dev.idb / usr / share / Insight / bin / glossQA.z / glossQA
Text File  |  2002-10-15  |  6KB  |  309 lines

  1. #!/usr/bin/perl5
  2.  
  3. ####################################################################
  4. #
  5. #  Name: glossQA
  6. #
  7. #  Note: PERL 5 or greater is required for this script.
  8. #
  9. #  Function: Scan an SGML file to find all instances of glossary terms.
  10. #    Compare to determine if term exists in SGML; check global glossary 
  11. #    if specified/available.
  12. #
  13. #  Author: Ferg (gferg@sgi.com) / Adrian Daley (agd@sgi.com)
  14. #
  15. #  Other Information:
  16. #
  17. #    SGIDOCBK Glossary -
  18. #
  19. #    <glossterm> tags also appear within the content of the book
  20. #
  21. #    <glossentry>
  22. #        <glossterm></glossterm>
  23. #        <glossdef></glossdef>
  24. #    </glossentry>
  25. #
  26. #
  27. #    SGIDOC Glossary -
  28. #
  29. #    <glossaryitem> occurs throughout the book
  30. #
  31. #    <glossaryterm>
  32. #        <glossaryentry></glossaryentry>
  33. #        <glossarydef></glossarydef>
  34. #    </glossaryterm>
  35. #
  36. #
  37. #    SGIDOC          SGIDOCBK
  38. #    ---------       --------------
  39. #    glossaryitem    glossterm
  40. #    glossaryentry   glossentry
  41. #    glossaryterm    glossterm
  42. #    glossarydef     glossdef
  43. #
  44. #
  45. #  Version 1.1 - 02Mar99
  46. #    Updated old code; ability to recognize/use sgidocbk or sgidoc
  47. #
  48. ####################################################################
  49.  
  50.  
  51. my($fname, $gg, $arg, $s) = '';
  52. local(@ref_terms, @def_terms, @unres_terms) = ();
  53. my($ignore_gg)=0;
  54.  
  55. my($_GG) = '/hosts/bonnie.engr/depot/doc/1000/007-1859-060/gloss.sgm';
  56.  
  57. #  Check command-line args
  58. #
  59. while($arg = shift(@ARGV)) {
  60.     if ($arg =~ /^-h/) { 
  61.         usage('');
  62.     } elsif ($arg =~ /^-g/) { 
  63.         if (($gg = shift(@ARGV)) eq '') {
  64.             usage('');
  65.         }
  66.     } elsif ($arg =~ /^-ignore/) {
  67.         $ignore_gg = 1;
  68.     } elsif (!($arg =~ /^-/)) {
  69.         $fname = $arg;
  70.     }
  71. }
  72. if ($fname eq '') {
  73.     usage('');
  74. } elsif (!(-r $fname)) {
  75.     usage("glossQA: ERROR - cannot read $fname ($!)");
  76. }
  77.  
  78.  
  79. #  Global glossary location; note that cmd line spec overrides env var
  80. #
  81. if ($ignore_gg == 0) {
  82.  
  83.     if ($gg eq '') {
  84.         if ($ENV{GLOBAL_GLOSS}) {
  85.             $gg = $ENV{GLOBAL_GLOSS};
  86.         } else {
  87.             # XXX I don't like this at all...
  88.             #
  89.               $gg = $_GG;
  90.         }
  91.     }
  92.  
  93.     if (!(-r $gg)) {
  94.         print STDOUT "\nglossQA: warning - cannot read from ",
  95.             "global glossary : ($gg)\n";
  96.     }
  97. }
  98.  
  99.  
  100. #  Collect up all terms; put in our available lists:
  101. #
  102. #    ref_terms = referenced terms
  103. #    def_terms = defined terms
  104. #
  105. &collectTerms($fname);
  106. my($total_terms) = (@ref_terms + 0);
  107.  
  108. $fname =~ s#.*/##g;
  109. if ($total_terms == 0 ) {
  110.     print STDOUT "\nglossQA: No glossary terms were found in $fname\n";
  111.       exit(0);
  112. }
  113.  
  114.  
  115. #  See if there are any unresolved terms
  116. #
  117. &checkTerms();
  118.  
  119. undef (@ref_terms);
  120. undef (@def_terms);
  121.  
  122. #  No unresolved terms, exit; otherwise print them out as warnings if we
  123. #  are processing the global glossary (otherwise they get printed 2X)
  124. #
  125. if ((@unres_terms + 0) == 0 ) {
  126.     print STDOUT "\nglossQA: $total_terms glossary references found, ",
  127.         "0 undefined terms for $fname\n"; 
  128.       exit(0);
  129. } elsif ($ignore_gg == 0) {
  130.     print STDOUT "\nglossQA: warning - cannot find term (locally): ",
  131.         join("\nglossQA: warning - cannot find term (locally): ",
  132.             @unres_terms), "\n";
  133. }
  134. @ref_terms = (@unres_terms);
  135.  
  136.  
  137. #  Now collect terms from the global glossary
  138. #
  139. if ($ignore_gg == 0 && $gg ne '') {
  140.     print STDOUT "\nglossQA: checking global glossary ($gg)\n";
  141.     &collectTerms($gg);
  142.     &checkTerms();
  143. } else {
  144.     print STDOUT "\nglossQA: warning - ignoring the global glossary\n";
  145. }
  146.  
  147.  
  148. #  Report to user
  149. #
  150. print STDOUT "\nglossQA: $total_terms glossary references found, ", 
  151.              (@unres_terms + 0), " undefined terms for $fname\n";
  152.  
  153. if ((@unres_terms + 0) > 0 ) {
  154.     print STDOUT "\nglossQA: error - cannot find term: ",
  155.         join("\nglossQA: error - cannot find term: ", @unres_terms), 
  156.         "\n";
  157. }
  158.  
  159. exit(0);
  160.  
  161.  
  162. ### END MAIN PROGRAM ###
  163.  
  164.  
  165.  
  166. ####################################################################
  167. #
  168. # void collectTerms(string $input_file)
  169. #
  170. #    given an input file, collect up all referenced glossary
  171. #    terms AND any defined entries
  172. #
  173. #    ref_terms = referenced terms
  174. #    def_terms = defined terms
  175. #
  176. ####################################################################
  177.  
  178. sub collectTerms {
  179.  
  180.     my($fname) = @_;
  181.  
  182.     my($pat)   = 'GLOSSARYITEM|GLOSSTERM|GLOSSARYENTRY';
  183.     my($g_pat) = 'GLOSSARY';
  184.     my($val,$s)= '';
  185.         my($found_gloss) = 0;
  186.  
  187.     #  Read in specified file
  188.     #
  189.     open(FH, $fname) or usage("glossQA: ERROR - cannot open $fname ($!)");
  190.     while (<FH>) {
  191.  
  192.         if ($found_gloss == 0 && /<$g_pat>/i) {
  193.             $found_gloss = 1;
  194.         }
  195.  
  196.         #  Look for terms within book for both DTD types.
  197.         #
  198.         #  We know whether or not we are inside the <glossary>,
  199.         #  and we push the term onto the appropriate list
  200.         #
  201.         while (s/<($pat)[^>]*>(.*?)<\/($pat)>/$1/i) {
  202.  
  203.             ($val = $2) =~ s/\\/\\\\/;
  204.             $val =~ s/\(/\\\(/;
  205.             $val =~ s/\)/\\\)/;
  206.             $val =~ s/\&\w+\;//g;
  207.  
  208.             # remove extra whitespace
  209.             $val =~ s/^\s+|\s+$//g;
  210.  
  211.             #  With international character-sets, we could
  212.             #  not use grep() effectively, hence the eq check 
  213.             #
  214.             if( $found_gloss == 0 ) {
  215.  
  216.                 foreach $s (@ref_terms) {
  217.                     if($s eq $val) {
  218.                         $val = '';
  219.                         last;
  220.                     }
  221.                 }
  222.                 if ($val ne '') {
  223.                     push(@ref_terms, $val);
  224.                 }
  225.             } else {
  226.  
  227.                 foreach $s (@def_terms) {
  228.                     if($s eq $val) {
  229.                         $val = '';
  230.                         last;
  231.                     }
  232.                 }
  233.                 if ($val ne '') {
  234.                     push(@def_terms, $val);
  235.                 }
  236.             }
  237.         }
  238.     }
  239.     close(FH);
  240. }
  241.  
  242.  
  243.  
  244. ####################################################################
  245. #
  246. # void checkTerms()
  247. #
  248. #    Look at all referenced terms, see if available in def terms,
  249. #    if not, then push onto our list of unresolved terms
  250. #
  251. ####################################################################
  252.  
  253. sub checkTerms {
  254.  
  255.     @unres_terms = ();
  256.  
  257.     my($val,$s) = '';
  258.     foreach $val (@ref_terms) {
  259.  
  260.         #  With international character-sets, we could
  261.         #  not use grep() effectively, hence the eq check 
  262.         #
  263.         foreach $s (@def_terms) {
  264.             if($s eq $val) {
  265.                 $val = '';
  266.                 last;
  267.             }
  268.         }
  269.         if ($val ne '') {
  270.             push(@unres_terms, $val);
  271.         }
  272.     }
  273. }
  274.  
  275.  
  276.  
  277. ####################################################################
  278. #
  279. #    Prints the program's usage statement and exits.
  280. #    Pass in a msg string to print out, in event of error
  281. #
  282. ####################################################################
  283.  
  284. sub usage {
  285.  
  286.     my($msg)  = @_;
  287.     my($name) = $0;
  288.     $name =~ s#.*/##g;
  289.  
  290. print <<END_USAGE;
  291.  
  292. $name Version 1.1
  293.  
  294. Usage: $name [-h] [-g <global_gloss> | -ignore] [<input_file>]
  295.  
  296.   -h                Print this help message
  297.   -g <global_gloss> Name/path to global glossary SGML file
  298.   -ignore           Ignore the global glossary file (default is to use one)
  299.   <input_file>      SGML file to check
  300.  
  301. END_USAGE
  302.  
  303.     if ($msg ne '') {
  304.         print STDERR "\n$msg\n";
  305.     }
  306.     exit(0);
  307. }
  308.  
  309.